package org.szgb.console.sys.line.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.sys.equipment.entity.SysEquipment;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysLineLocationSystemEquipment extends BeanEntity<SysLineLocationSystemEquipment> {
    @Serial
    private static final long serialVersionUID = 1L;
    private SysLineLocationSystem sysLineLocationSystem;
    private SysEquipment sysEquipment;
    private String name;

    public SysLineLocationSystemEquipment() {
    }

    public SysLineLocationSystemEquipment(String id) {
        this.setId(id);
    }

    public SysLineLocationSystemEquipment(SysLineLocationSystemEquipment.SysLineLocationSystemEquipmentFindDto sysLineLocationSystemEquipmentFindDto) {
        this.setId(sysLineLocationSystemEquipmentFindDto.getId());
        this.setSysLineLocationSystem(new SysLineLocationSystem(sysLineLocationSystemEquipmentFindDto.getSysLineLocationSystemId()));
        this.setSysEquipment(new SysEquipment(sysLineLocationSystemEquipmentFindDto.getEquipmentId()));
        this.setPageDto(sysLineLocationSystemEquipmentFindDto.getPageDto());
    }

    public SysLineLocationSystemEquipment(SysLineLocationSystemEquipment.SysLineLocationSystemEquipmentSaveDto sysLineLocationSystemEquipmentSaveDto) {
        this.setId(sysLineLocationSystemEquipmentSaveDto.getId());
        this.setSysLineLocationSystem(new SysLineLocationSystem(sysLineLocationSystemEquipmentSaveDto.getSysLineLocationSystemId()));
        this.setSysEquipment(new SysEquipment(sysLineLocationSystemEquipmentSaveDto.getEquipmentId()));
    }

    public SysLineLocationSystemEquipment.SysLineLocationSystemEquipmentView toSysLineLocationSystemEquipmentView() {
        return new SysLineLocationSystemEquipment.SysLineLocationSystemEquipmentView(this);
    }

    @Data
    @Schema(title = "线路站点系统设备信息", description = "线路站点系统设备信息")
    public static class SysLineLocationSystemEquipmentView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "站点系统id")
        private String sysLineLocationSystemId;
        @Schema(description = "站点系统名称")
        private String sysLineLocationSystemName;
        @Schema(description = "设备id")
        private String equipmentId;
        @Schema(description = "设备名称")
        private String equipmentName;
        @Schema(description = "名称")
        private String name;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysLineLocationSystemEquipmentView(SysLineLocationSystemEquipment sysLineLocationSystemEquipment) {
            this.id = sysLineLocationSystemEquipment.getId();
            this.sysLineLocationSystemId = sysLineLocationSystemEquipment.getSysLineLocationSystem().getId();
            this.sysLineLocationSystemName = sysLineLocationSystemEquipment.getSysLineLocationSystem().getSysSystem().getName();
            this.equipmentId = sysLineLocationSystemEquipment.getSysEquipment().getId();
            this.equipmentName = sysLineLocationSystemEquipment.getSysEquipment().getName();
            this.name = sysLineLocationSystemEquipment.getName();
            this.createdAt = sysLineLocationSystemEquipment.getCreatedAt();
            this.updatedAt = sysLineLocationSystemEquipment.getUpdatedAt();
            if (sysLineLocationSystemEquipment.getCreatedBy() != null) {
                this.createdById = sysLineLocationSystemEquipment.getCreatedBy().getId();
                this.createdByName = sysLineLocationSystemEquipment.getCreatedBy().getName();
            }
            if (sysLineLocationSystemEquipment.getUpdatedBy() != null) {
                this.updatedById = sysLineLocationSystemEquipment.getUpdatedBy().getId();
                this.updatedByName = sysLineLocationSystemEquipment.getUpdatedBy().getName();
            }
        }

        public static List<SysLineLocationSystemEquipment.SysLineLocationSystemEquipmentView> getViewListFromList(List<SysLineLocationSystemEquipment> sysLineLocationSystemEquipmentList) {
            return sysLineLocationSystemEquipmentList.stream().map(SysLineLocationSystemEquipment.SysLineLocationSystemEquipmentView::new).toList();
        }
    }

    @Data
    @Schema(title = "线路站点系统设备树表子项信息", description = "线路站点系统设备树表子项信息")
    public static class SysLineLocationSystemEquipmentTreeView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "设备名称")
        private String name;

        public SysLineLocationSystemEquipmentTreeView(SysLineLocationSystemEquipment sysLineLocationSystemEquipment) {
            this.id = sysLineLocationSystemEquipment.getId();
            this.name = sysLineLocationSystemEquipment.getName();
        }

        public static List<SysLineLocationSystemEquipment.SysLineLocationSystemEquipmentTreeView> getViewListFromList(List<SysLineLocationSystemEquipment> sysLineLocationSystemEquipmentList) {
            return sysLineLocationSystemEquipmentList.stream().map(SysLineLocationSystemEquipment.SysLineLocationSystemEquipmentTreeView::new).toList();
        }
    }

    @Data
    @Schema(title = "线路站点系统查询表单", description = "线路站点系统查询参数")
    public static class SysLineLocationSystemEquipmentFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "站点系统id")
        private String sysLineLocationSystemId;
        @Schema(description = "设备id")
        private String equipmentId;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysLineLocationSystemEquipment toSysLineLocationSystemEquipment() {
            return new SysLineLocationSystemEquipment(this);
        }
    }

    @Data
    @Schema(title = "线路站点系统保存表单", description = "线路站点系统保存参数")
    public static class SysLineLocationSystemEquipmentSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "站点系统id")
        private String sysLineLocationSystemId;
        @Schema(description = "设备id")
        private String equipmentId;
        @Schema(description = "名称")
        private String name;

        public SysLineLocationSystemEquipment toSysLineLocationSystemEquipment() {
            return new SysLineLocationSystemEquipment(this);
        }
    }
}
